// MyWindowDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MyWindow.h"
#include "MyWindowDlg.h"
#include "MyUtils.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CMyWindowDlg dialog




CMyWindowDlg::CMyWindowDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyWindowDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_clickedTimes = 0;
	m_clickInMouseDown = false;
	m_lastWnd = NULL;
	m_wndx = m_wndy = 0;
}

void CMyWindowDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_MOUSE_DOWN, m_wndMouseDown);
	DDX_Control(pDX, IDC_HWND_TEXT, m_wndHWNDText);
	DDX_Control(pDX, IDC_HWND_COORD, m_wndHWNDCoord);
}

BEGIN_MESSAGE_MAP(CMyWindowDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDOK, &CMyWindowDlg::OnBnClickedOk)
	ON_BN_CLICKED(IDCANCEL, &CMyWindowDlg::OnBnClickedCancel)
	ON_WM_CLOSE()
//	ON_BN_CLICKED(IDC_BUTTON1, &CMyWindowDlg::OnBnClickedButton1)
//	ON_BN_CLICKED(IDC_BUTTON2, &CMyWindowDlg::OnBnClickedButton2)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_BN_CLICKED(IDC_BUTTON1, &CMyWindowDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CMyWindowDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CMyWindowDlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON4, &CMyWindowDlg::OnBnClickedButton4)
	ON_BN_CLICKED(IDC_BUTTON5, &CMyWindowDlg::OnBnClickedButton5)
	ON_BN_CLICKED(IDC_BUTTON6, &CMyWindowDlg::OnBnClickedButton6)
	ON_BN_CLICKED(IDC_BUTTON7, &CMyWindowDlg::OnBnClickedButton7)
	ON_BN_CLICKED(IDC_BUTTON8, &CMyWindowDlg::OnBnClickedButton8)
	ON_BN_CLICKED(IDC_BUTTON9, &CMyWindowDlg::OnBnClickedButton9)
END_MESSAGE_MAP()


// CMyWindowDlg message handlers

BOOL CMyWindowDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	m_llkprocessor.LoadMetaImages();

	CreateDirectory(TEXT("metaimages"), 0);
	CreateDirectory(TEXT("saveimages"), 0);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyWindowDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMyWindowDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMyWindowDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CMyWindowDlg::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	OnOK();
}

void CMyWindowDlg::OnBnClickedCancel()
{
	// TODO: Add your control notification handler code here
	OnCancel();
}

void CMyWindowDlg::OnClose()
{
	// TODO: Add your message handler code here and/or call default

	CDialog::OnClose();
}

//void CMyWindowDlg::OnBnClickedButton1()
//{
//	// TODO: Add your control notification handler code here
//	m_clickedTimes += 1;
//	updateClickTimes();
//}

//void CMyWindowDlg::OnBnClickedButton2()
//{
//	// TODO: Add your control notification handler code here
//	m_clickedTimes -= 1;
//	updateClickTimes();
//}

void CMyWindowDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	testClickInMouseDown(nFlags, point);

	CDialog::OnLButtonDown(nFlags, point);
}

void CMyWindowDlg::testClickInMouseDown(UINT nFlags, CPoint point)
{
	if (m_clickInMouseDown)
	{
		return;
	}

	CPoint ptClick;
	CRect rtWnd;

	ptClick = point;

	ClientToScreen(&ptClick);

	m_wndMouseDown.GetWindowRect(& rtWnd);

	if (rtWnd.PtInRect(ptClick))
	{
		m_clickInMouseDown = true;
		SetCursor(LoadCursor(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDC_CURSOR1)));
		SetCapture();
	}
}
void CMyWindowDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default

	m_clickInMouseDown = false;
	SetCursor(NULL);
	ReleaseCapture();

	m_getWndInfo.EraseLastWindow();

	CDialog::OnLButtonUp(nFlags, point);
}

void CMyWindowDlg::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	if(m_clickInMouseDown && !testPointInDialog(nFlags, point))
	{
		CString _tmpstr;
		CRect _rtwnd;
		m_getWndInfo.GetWindowFromCursorInfo();

		m_lastWnd = m_getWndInfo.m_WinInfo.hWnd;
		::GetWindowRect(m_lastWnd, &_rtwnd);

		_tmpstr.Format(TEXT("%s"), m_getWndInfo.m_WinInfo.szWindowCaption);
		m_wndHWNDText.SetWindowText(_tmpstr);
 		_tmpstr.Format(TEXT("%d %d %d %d"), _rtwnd.left, _rtwnd.top, _rtwnd.right, _rtwnd.bottom);
		m_wndx = _rtwnd.left + 15 + 3;
		m_wndy = _rtwnd.top + 181;
 		m_wndHWNDCoord.SetWindowText(_tmpstr);
	}

	CDialog::OnMouseMove(nFlags, point);
}

BOOL CMyWindowDlg::testPointInDialog(UINT nFlags, CPoint point)
{
	CPoint ptClick;
	CRect rtWnd;

	ptClick = point;

	ClientToScreen(&ptClick);

	this->GetWindowRect(&rtWnd);

	return rtWnd.PtInRect(ptClick);
}
void CMyWindowDlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	m_llkprocessor.TopmostWindow(m_lastWnd);
	m_llkprocessor.PrintScreen(m_wndx, m_wndy, m_lastWnd, true);
	m_llkprocessor.TopmostWindow(m_hWnd);
	m_llkprocessor.Updateexevec();
	m_llkprocessor.ExeAll();
}

void CMyWindowDlg::OnBnClickedButton2()
{
	// TODO: Add your control notification handler code here
	m_llkprocessor.PrintMatrix();
}

void CMyWindowDlg::OnBnClickedButton3()
{
	// TODO: Add your control notification handler code here
	m_llkprocessor.LoadMetaImages();
}

void CMyWindowDlg::OnBnClickedButton4()
{
	// TODO: Add your control notification handler code here
// 	m_llkprocessor.ClearMatrixNMap();
	m_llkprocessor.TopmostWindow(m_lastWnd);
	m_llkprocessor.PrintScreen(m_wndx, m_wndy, m_lastWnd, true);
	m_llkprocessor.TopmostWindow(m_hWnd);
}

void CMyWindowDlg::OnBnClickedButton5()
{
	// TODO: Add your control notification handler code here
	m_llkprocessor.PrintBMPMap();
}

void CMyWindowDlg::OnBnClickedButton6()
{
	// TODO: Add your control notification handler code here
	m_llkprocessor.TesetFunc();
}

void CMyWindowDlg::OnBnClickedButton7()
{
	// TODO: Add your control notification handler code here
	m_llkprocessor.Updateexevec();
}

void CMyWindowDlg::OnBnClickedButton8()
{
	// TODO: Add your control notification handler code here
	m_llkprocessor.ExeAll();
}

void CMyWindowDlg::OnBnClickedButton9()
{
	// TODO: Add your control notification handler code here
	m_llkprocessor.TestClick();
}
